package edu.suda.xsap.dto.convert;

import edu.suda.xsap.dto.MemberCardDto;
import edu.suda.xsap.dto.MemberDto;
import edu.suda.xsap.dto.MemberWithCardsDto;
import edu.suda.xsap.entity.Member;
import edu.suda.xsap.entity.MemberCard;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.sql.Date;
import java.util.List;

@Mapper(componentModel = "spring")
public interface MemberConvert {

    @Mapping(source = "id", target = "id")
    @Mapping(source = "name", target = "name")
    @Mapping(source = "sex", target = "sex")
    @Mapping(source = "phone", target = "phone")
    @Mapping(source = "birthday", target = "birthday")
    @Mapping(source = "note", target = "note")
    @Mapping(source = "avatarUrl", target = "avatarUrl")
    @Mapping(source = "isDeleted", target = "isDeleted")
    @Mapping(source = "createTime", target = "createTime")
    @Mapping(source = "lastModifyTime", target = "lastModifyTime")
    @Mapping(source = "version", target = "version")
    MemberDto toDto(Member member);

    List<MemberDto> toDtoList(List<Member> members);

    // 手动实现转换 MemberWithCardsDto 的方法
    default MemberWithCardsDto toWithCardsDto(MemberDto memberDto, List<MemberCardDto> memberCardDtos) {
        MemberWithCardsDto memberWithCardsDto = new MemberWithCardsDto();
        memberWithCardsDto.setId(memberDto.getId());
        memberWithCardsDto.setName(memberDto.getName());
        memberWithCardsDto.setSex(memberDto.getSex());
        memberWithCardsDto.setPhoneNumber(memberDto.getPhone());
        //如果生日为空，则设置为null，否则设置为Date类型
        if (memberDto.getBirthday() == null) {
            memberWithCardsDto.setBirthDay(null);
        } else {
            memberWithCardsDto.setBirthDay(Date.valueOf(memberDto.getBirthday()));
        }

        memberWithCardsDto.setNote(memberDto.getNote());
        memberWithCardsDto.setMemberCards(memberCardDtos);
        return memberWithCardsDto;
    }

    MemberCardDto toCardDto(MemberCard memberCard);

    // 用于将前端数据转为Member类，不知道能否使用
    Member toEntity(MemberDto memberDto);

}